Completed
Push — master ( 87deee...af9e24 )
by Andres
41s
created

elements.js ➔ ... ➔ ct.getbuyAmount   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 4
rs 10
nop 1
1
/**
2
 elements
3
 Component that handles the periodic table tab.
4
 It includes the logic to purchase and display elements.
5
6
 @namespace Components
7
 */
8
'use strict';
9
10
angular.module('game').component('elements', {
11
  templateUrl: 'views/elements.html',
12
  controller: ['$timeout', 'state', 'data', 'util', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements($timeout, state, data, util) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.outcome = {};
22
  ct.buyAmount = [1, 10, 25, 100, 1000];
23
24
  ct.getChance = function(element) {
25
    let bonus = 0;
26
    for(let resource of data.elements[element].includes){
27
      bonus += state.player.resources[resource].number*data.constants.ELEMENT_CHANCE_BONUS;
28
    }
29
    let singleChance = data.elements[element].abundance*(1+bonus);
30
    let chance = 1 - Math.pow(Math.max(0, 1-singleChance), Math.min(state.player.resources.dark_matter.number, ct.buyAmount[state.player.options.elementBuyIndex]));
31
32
    return Math.min(1, chance);
33
  };
34
35
  ct.buyElement = function (element) {
36
    if (state.player.elements[element]) {
37
      return;
38
    }
39
    if(Math.random() < ct.getChance(element)){
40
      state.player.elements[element] = true;
41
      state.player.exotic_upgrades[element] = {};
42
      for(let up in data.exotic_upgrades){
43
        state.player.exotic_upgrades[element][up] = false;
44
      }
45
      state.player.elements_unlocked++;
46
      ct.outcome[element] = 'Success';
47
    }else{
48
      ct.outcome[element] = 'Fail';
49
    }
50
    state.player.resources.dark_matter.number -= Math.min(state.player.resources.dark_matter.number, ct.buyAmount[state.player.options.elementBuyIndex]);
51
52
    $timeout(function(){ct.clearMessage(element);}, 1000);
53
  };
54
55
  ct.clearMessage = function (element) {
56
    ct.outcome[element] = '';
57
  };
58
59
  /* This function returns the class that determines on which
60
  colour an element card */
61
  ct.elementClass = function (element) {
62
    if (state.player.elements[element]) {
63
      return 'element_purchased';
64
    }
65
    if (isElementAvailable(element)) {
66
      return 'element_available';
67
    }
68
    return 'element_unavailable';
69
  };
70
71
  function isElementAvailable(element) {
72
    for(let resource of data.elements[element].includes){
73
      if(state.player.resources[resource].unlocked){
74
        return true;
75
      }
76
    }
77
    return false;
78
  }
79
80
  /* This function returns the class that determines the secondary
81
  colour of an element card */
82
  ct.elementSecondaryClass = function (element) {
83
    return ct.elementClass(element) + '_dark';
84
  };
85
}
86